﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Kernel.WindowsEx;

namespace Krile.Forms.PManForm
{
    public partial class PluginManager : Form
    {
        public PluginManager()
        {
            InitializeComponent();
        }

        List<PluginElement> elems = new List<PluginElement>();
        private void PluginManager_Load(object sender, EventArgs e)
        {
            var pdArray = Core.PluginManager.GetAllPluginDatas();
            if (pdArray != null && pdArray.Length > 0)
            {
                foreach (var pd in pdArray)
                {
                    var sp = new Panel() { Height = 1, BackColor = SystemColors.ControlDarkDark };
                    pluginsPanel.Controls.Add(sp);
                    sp.Dock = DockStyle.Top;
                    var newpe = new PluginElement(pd);
                    pluginsPanel.Controls.Add(newpe);
                    newpe.Dock = DockStyle.Top;
                    elems.Add(newpe);
                }
            }
            else
            {
                MessageBoxEx.Show(
                    Lang.Msg.PMan_PluginNotFound_Context, Lang.Msg.PMan_PluginNotFound,
                    Lang.Msg.PMan_PluginNotFound_Caption,
                    MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Error);
                this.Close();
            }
        }

        private void PluginManager_Shown(object sender, EventArgs e)
        {
            pluginsPanel.AutoScrollPosition = new Point(0, 0);
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkUpdate_Click(object sender, EventArgs e)
        {
            checkUpdate.Enabled = false;
            var act = new Action(checkUpdInvoke);
            act.BeginInvoke((iar) =>
            {
                ((Action)iar.AsyncState).EndInvoke(iar);
                this.SafeOperate(() => checkUpdate.Enabled = true);
            }, act);
        }

        private void checkUpdInvoke()
        {
            foreach (var e in elems)
            {
                if (e.PluginData.UpdateCheck != null)
                {
                    var nc = new Krile.Common.NetConnect();
                    var ret = nc.WebConnectDownloadString(e.PluginData.UpdateCheck, "GET", null);
                    double newV;
                    if (ret.Succeeded && double.TryParse(ret.Data, out newV))
                    {
                        if (e.PluginData.Version < newV)
                        {
                            this.SafeOperate(() => e.UpdateState = PluginElement.UpdateStates.Exists);
                        }
                        else
                        {
                            this.SafeOperate(() => e.UpdateState = PluginElement.UpdateStates.None);
                        }
                    }
                    else
                    {
                        this.SafeOperate(() => e.UpdateState = PluginElement.UpdateStates.Error);
                    }
                }
                else
                {
                    this.SafeOperate(() => e.UpdateState = PluginElement.UpdateStates.Error);
                }
            }
        }

        private void PluginManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            Core.SaveConfig(true);
        }
    }
}
